package com.android.jblm.commandcentral;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListView;

import java.util.List;

import com.android.jblm.commandcentral.adapters.NewsAdapter;
import com.android.jblm.commandcentral.generators.NewsBuilderNON_ASYNC;
import com.android.jblm.commandcentral.objects.Post;

/**
 * Created by Brian 2014
 *
 * Reads RSS feed from website and lists posts by title and date with expandable
 * sub-views containing brief description and web link.
 *
 * Based on example @http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
 */
public class News extends Activity {

    NewsAdapter listAdapter;//Adapter to handle Post View creation
    ExpandableListView listView;//View widget to display posts from adapter

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);

        //Creates and executes asynchronous activity to fetch posts
        new InnerNewsBuilder().execute("");

    }

    /**
     * Receives a list of Post objects to display using adapter
     * and expandable list view activities.
     *
     * This method is called by inner class innerNewsBuilder
     *
     * @param theList List of Post objects
     */
    protected void setupListView(List<Post> theList){

        //Expandable list view widget newsList in news layout
        listView = (ExpandableListView) findViewById(R.id.newsList);
        Log.w("NewsActivity","Creating Adapter");
        listAdapter = new NewsAdapter(this, theList);
        Log.w("NewsActivity","Assigning Adapter");
        listView.setAdapter(listAdapter);
    }

    /**
     * Not currently used, may be useful for updates of file source
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.news, menu);
        return true;
    }

    /**
     * Used by options menu, which agian is not currently used
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Asynchronous inner class that handles fetching of posts in separate thread
     * onPostExecute is called when post thread completes, passing the list
     * back to the method setupListView
     */
    class InnerNewsBuilder extends AsyncTask<String, Integer, List<Post>>{


        protected List<Post> doInBackground(String... params) {
            NewsBuilderNON_ASYNC list = new NewsBuilderNON_ASYNC();

            return list.getThePosts();
        }

        @Override
        protected void onPostExecute(List<Post> result){
            //TODO

            setupListView(result);

        }

        @Override
        protected void onProgressUpdate(Integer... values){

            //Nothing
        }
    }

}
